package com.china08.yxyapi.api.manage.sch.platform.statistics.sws.swsschool;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.sws.SwsPvForDailyAnalyze;
import com.china08.yxyapi.entity.mg.sws.SwsPvForTotalAnalyze;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.SwsPvForDailyAnalyzeRepository;
import com.china08.yxyapi.repository.mg.SwsPvForTotalAnalizeRepository;
import com.china08.yxyapi.util.DateUtils;
@CrossOrigin
@RestController
public class SwsSchoolStatictisCountAllTwoController extends YxyApiBaseController{
    
    @Autowired
    private SwsPvForDailyAnalyzeRepository  swsPvForDailyAnalyzeRepository;
    @Autowired
    private SwsPvForTotalAnalizeRepository swsPvForTotalAnalizeRepository;
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/sch/platform/statistics/sws/swsschool/countall/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response) {
       RespModel model = new RespModel();
       String schoolId = getProfile(request, response).getSchoolId();
       Calendar c = Calendar.getInstance();
       int year = c.get(Calendar.YEAR); 
       int month = c.get(Calendar.MONTH)+1; 
       int date = c.get(Calendar.DATE); 
       SwsPvForDailyAnalyze swsPvForDailyAnalyze = swsPvForDailyAnalyzeRepository.findBySchoolIdAndYearForPvAndMonthForPvAndDayForPv(schoolId, year, month, date);  
       Long datePv = swsPvForDailyAnalyze.getPv();//当日点击量
       model.datePv = datePv;
       
       
       SwsPvForDailyAnalyze swsPvForDailyAnalyze2 = null;
       Long pv = 0l;
       Date weekFirst = null;
       List<String> weekList = new ArrayList<String>();
       for (int i = 0; i < 7; i++) {        
           weekFirst = DateUtils.getThisWeek(new Date(),i);//获取本周从周日到周六的日期
           if (weekFirst != null) {            
               weekList.add(DateUtils.formatDate4(weekFirst).toString());
           }
       }
       for (String old : weekList) {
           swsPvForDailyAnalyze2 = swsPvForDailyAnalyzeRepository.findBySchoolIdAndYearForPvAndMonthForPvAndDayForPv(schoolId, Integer.valueOf(old.substring(0,4)), Integer.valueOf(old.substring(4, 6)), Integer.valueOf(old.substring(6)));
           pv +=swsPvForDailyAnalyze2.getPv();
       }
       model.weekPv = pv;
       
       //获取当前月
       List<SwsPvForDailyAnalyze> swsPvForDailyAnalyzeLst = swsPvForDailyAnalyzeRepository.findBySchoolIdAndYearForPvAndMonthForPv(schoolId, year, month);
       Long pvMonth = 0l;
       for (SwsPvForDailyAnalyze swsPvForDailyAnalyzeMonth : swsPvForDailyAnalyzeLst) {
           pvMonth += swsPvForDailyAnalyzeMonth.getPv();
       }
       model.monthPv = pvMonth;
       
       //获取总点击量
       SwsPvForTotalAnalyze swsPvForTotalAnalyzeTotal = swsPvForTotalAnalizeRepository.findBySchoolId(schoolId); 
       model.allPv = swsPvForTotalAnalyzeTotal.getPv();
       return model;   
    }
    protected static class RespModel extends YxyApiRespModel {
        //当日点击量
        public Long datePv;
        //本周点击量
        public Long weekPv;
        //本月点击量
        public Long monthPv;
        //总点击量
        public Long allPv;
    }
}
